<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/19
 * Time: 10:27
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Aftersailreason extends Base
{
    /**
     * 售后原因列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('aftersail_reason')->where('ar_status', 1)->count();// 获取总条数
            $lists = Db::name('aftersail_reason')
                ->where('ar_status', 1)
                ->field('ar_id, ar_name, ar_time, ar_update_time')
                ->page($Nowpage,$limits)
                ->order('ar_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加售后原因
     * @return mixed|\think\response\Json
     */
    public function reasonAdd(){
        if(Request::instance()->isPost()){
            $ar_name = trim(input('ar_name'), ' ');
            $exist = Db::table('up_aftersail_reason')->where('ar_name', $ar_name)->find();
            if($exist) return json(['code' => 100, 'msg' => '原因重复']);
            
            $res = Db::table('up_aftersail_reason')->insert([
                'ar_name' => $ar_name,
                'ar_time' => date('Y-m-d H:i:s'),
                'ar_update_time' => date('Y-m-d H:i:s')
            ]);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('aftersailreason/reasonadd');
    }
    
    /**
     * 编辑售后原因
     * @return mixed|\think\response\Json
     */
    public function reasonEdit(){
        $ar_id = input('ar_id');
        if(Request::instance()->isPost()){
            $ar_name = trim(input('ar_name'), ' ');
            $exist = Db::table('up_aftersail_reason')
                ->where('ar_name', $ar_name)
                ->where('ar_id', 'NEQ', $ar_id)
                ->find();
            if($exist) return json(['code' => 100, 'msg' => '原因重复']);
            
            $res = Db::table('up_aftersail_reason')
                ->where('ar_id', $ar_id)
                ->update([
                    'ar_name' => $ar_name,
                    'ar_update_time' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $reason = Db::table('up_aftersail_reason')->where('ar_id', $ar_id)->value('ar_name');
        $this->assign([
            'ar_id' => $ar_id,
            'reason' => $reason
        ]);
        return $this->fetch('aftersailreason/reasonedit');
    }
    
    /**
     * 删除售后原因
     * @return \think\response\Json
     */
    public function reasonDel(){
        $ar_id = input('id');
        $res = Db::table('up_aftersail_reason')->where('ar_id', $ar_id)->setField('ar_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}